package com.vincent.template.commons.validation.constraints;

import com.vincent.template.commons.validation.validators.UniqueEntityValidator;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 校验泛型为实体对象的list是否重复，需要传入指定的字段名fields构建唯一规则（参与重复判定的字段必须实现get方法）
 * @Author Vincent Wang
 * @Date 2020/11/4 13:52
 */
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(ReplicateUniqueEntity.class)
@Constraint(validatedBy = {UniqueEntityValidator.class})
public @interface UniqueEntity {

    String[] fields();

    /** 忽略多个字段的值全部为空时的重复 */
    boolean ignoreAllEmpty() default true;

    String message() default "列表元素字段{fields}必须联合唯一";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
}
